package com.sugar.follow.up.module.business.controller.admin.userpatient;

import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.excel.core.util.ExcelUtils;
import com.sugar.follow.up.module.business.controller.admin.userpatient.vo.UserPatientDoctorAllotReqVO;
import com.sugar.follow.up.module.business.controller.admin.userpatient.vo.UserPatientPageReqVO;
import com.sugar.follow.up.module.business.controller.admin.userpatient.vo.UserPatientRespVO;
import com.sugar.follow.up.module.business.controller.admin.userpatient.vo.UserPatientSaveReqVO;
import com.sugar.follow.up.module.business.service.userpatient.UserPatientService;
import com.sugar.follow.up.module.system.controller.admin.user.vo.user.UserSimpleRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 就诊人")
@RestController
@RequestMapping("/business/user-patient")
@Validated
public class UserPatientController {

    @Resource
    private UserPatientService userPatientService;

    @PutMapping("/update")
    @Operation(summary = "更新就诊人")
    @PreAuthorize("@ss.hasPermission('business:user-patient:update')")
    public CommonResult<Boolean> updateUserPatient(@Valid @RequestBody UserPatientSaveReqVO updateReqVO) {
        userPatientService.updateUserPatient(updateReqVO);
        return success(true);
    }

    @PutMapping("/doctor/allot")
    @Operation(summary = "分配医生")
    @PreAuthorize("@ss.hasPermission('business:user-patient:doctor.allot')")
    public CommonResult<Boolean> userPatientDoctorAllot(@Valid @RequestBody UserPatientDoctorAllotReqVO doctorAllotReqVO) {
        userPatientService.userPatientDoctorAllot(doctorAllotReqVO);
        return success(true);
    }

    @GetMapping("/doctors")
    @Operation(summary = "获取所有医生")
    @PreAuthorize("@ss.hasPermission('business:user-patient:doctor.allot')")
    public CommonResult<List<UserSimpleRespVO>> getDoctors() {
        return success(userPatientService.getDoctors());
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除就诊人")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:user-patient:delete')")
    public CommonResult<Boolean> deleteUserPatient(@RequestParam("id") Long id) {
        userPatientService.deleteUserPatient(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除就诊人")
                @PreAuthorize("@ss.hasPermission('business:user-patient:delete')")
    public CommonResult<Boolean> deleteUserPatientList(@RequestParam("ids") List<Long> ids) {
        userPatientService.deleteUserPatientListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得就诊人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:user-patient:query')")
    public CommonResult<UserPatientRespVO> getUserPatient(@RequestParam("id") Long id) {
        return success(userPatientService.backGetUserPatient(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得就诊人分页")
    @PreAuthorize("@ss.hasPermission('business:user-patient:query')")
    public CommonResult<PageResult<UserPatientRespVO>> getUserPatientPage(@Valid UserPatientPageReqVO pageReqVO) {
        return success(userPatientService.getUserPatientPage(pageReqVO));
    }

    @PostMapping("/people")
    @Operation(summary = "获得标签下的就诊人数量")
    @PreAuthorize("@ss.hasPermission('business:user-patient:query')")
    public CommonResult<Integer> getTagsPatientCount(@RequestBody UserPatientPageReqVO pageReqVO) {
        return success(userPatientService.execGeneratorSql(pageReqVO.getTags()).size());
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出就诊人 Excel")
    @PreAuthorize("@ss.hasPermission('business:user-patient:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserPatientExcel(@Valid UserPatientPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "就诊人.xls", "数据", UserPatientRespVO.class,
                userPatientService.getUserPatientPage(pageReqVO).getList());
    }

}